package ywpt.service.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ClientInfo;
import ywpt.model.ClientType;
import ywpt.model.CommonTable;
import ywpt.model.Department;
import ywpt.web.common.PublicFunction;
@Service("clientService")
@RemoteProxy(name="clientService")
public class ClientServiceImpl extends HibernateEntityDao<ClientInfo> implements ClientService {
	public  ClientServiceImpl(){
		
	}
	
	@Override
	public String createClientCode() {
		DetachedCriteria query = DetachedCriteria.forClass(ClientInfo.class, "client");
		String clientCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<ClientInfo> clientInfos = this.getClientInfos(query);
		if ( clientInfos.iterator().hasNext() )	
		{
			code = (clientInfos.iterator().next().getClientCode()).substring(2);
			maxId = new Integer(code);
		}
		clientCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return ("KH"+clientCode);
	}


	@Override
	public ClientInfo getClientInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public int getClientInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientInfo> getClientInfos(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<ClientInfo> getClientInfos() {
		return this.getAll();
	}

	@Override
	public void removeClientInfo(Long id) {
		super.removeById(id);
	}

	@Override
	public void saveClientInfo(ClientInfo clientInfo,Long typeId) {
		if(typeId != null){
			clientInfo.setType(this.get(CommonTable.class, typeId));
		}
		super.save(clientInfo);
	}

	@Override
	public void updateClientInfo(ClientInfo clientInfo,Long typeId) {
		if(typeId != null){
			clientInfo.setType(this.get(CommonTable.class, typeId));
		}
		this.update(clientInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientInfo> getClientInfos(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<ClientInfo> getClientsByClientName(String clientName,Long departId, int start,
			int size) {
		DetachedCriteria query = DetachedCriteria.forClass(ClientInfo.class,"client");
//		if(departId!=null)
//			query.createAlias("depart", "depart").add(Restrictions.eq("depart.id", departId));
		
		if ( clientName != null && !clientName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("clientName", "%" + clientName + "%"),  
							          Restrictions.like("clientCode", "%" + clientName + "%")));
				// .add(Restrictions.eq("organType", "01"));
		}
		query.addOrder(Order.asc("clientCode"));
		List<ClientInfo> clientList = new ArrayList<ClientInfo>();
		List<ClientInfo> clientLists = new ArrayList<ClientInfo>();
		ClientInfo client =new ClientInfo();
		
		clientList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<ClientInfo> itr = clientList.iterator(); itr.hasNext(); i++ ) 
		{
			client = itr.next();
			String ss = PublicFunction.format(client.getClientCode() + "",7, "  ", 1)
			          + client.getClientName(); 
			
			client.setClientName(ss);
			clientLists.add(client);
		}
		return clientLists;
	}

	@Override
	@RemoteMethod
	public int getClientsCountByName(String clientName,Long departId) {
		DetachedCriteria query = DetachedCriteria.forClass(ClientInfo.class,"client");
//		if(departId!=null)
//			query.createAlias("depart", "depart").add(Restrictions.eq("depart.id", departId));
		if ( clientName != null && !clientName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("clientName", "%" + clientName + "%" ),  
							          Restrictions.like("clientCode", "%" + clientName + "%" )));
				 //.add(Restrictions.eq("organType", "01"));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Department> getDepartments() {
		String hql="select distinct(depart) from ClientInfo";
		return this.find(hql);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientType> getClientTypes() {
		String hql="select distinct(type) from ClientInfo";
		return this.find(hql);
	}

}
